/*
* ============================================================================
*  Name        : sea_event.cpp
*  Part of     : Finite state machine framework
*  Description : Event class implementation
*  Version     : %version: %
*  
*
* ============================================================================
*/
#include "sea_event.h"
#include "sea.h"

/**
 * @file sea_event.cpp
 * @brief
 * Generic event in state machine framework.
 *
 * @ingroup StateMachineFramework
 */
using namespace sea;

// -------------------------------------------------------------------------------------------------
// Ctor
// -------------------------------------------------------------------------------------------------
Event::Event( Event* event ):
    m_handle( event->get_state_machine( ) ),
    m_event_id( event->get_event_id( ) ),
    m_sync( event->is_sync( ) ),
    m_result( NULL )
{
    m_data = event->m_data;
    m_data_is_object = event->m_data_is_object;

    ASSERT_DEBUG( m_handle != NULL );
    if ( m_sync )
    {
        m_signal << NEW OsSemaphore;
    }

    reset_stamp( );
}

// -------------------------------------------------------------------------------------------------
// Ctor
// -------------------------------------------------------------------------------------------------
Event::Event( StateMachine* handle, EventId event_id, bool sync_op /*= false */ ):
    m_handle( handle ),
    m_event_id( event_id ),
    m_sync( sync_op ),
    m_result( NULL )
{
    m_data.m_void = NULL;
    m_data_is_object = false;

    ASSERT_DEBUG( handle );
    if ( m_sync )
    {
        m_signal << NEW OsSemaphore;
    }

    reset_stamp( );
}

// -------------------------------------------------------------------------------------------------
// Ctor
// -------------------------------------------------------------------------------------------------
Event::Event( StateMachine* handle, EventId event_id, void* data, bool sync_op /*= false */ ):
    m_handle( handle ),
    m_event_id( event_id ),
    m_sync( sync_op ),
    m_result( NULL )
{
    m_data.m_void = data;
    m_data_is_object = false;

    ASSERT_DEBUG( handle );
    if ( m_sync )
    {
        m_signal << NEW OsSemaphore;
    }

    reset_stamp( );
}

// -------------------------------------------------------------------------------------------------
// Ctor
// -------------------------------------------------------------------------------------------------
Event::Event( StateMachine* handle, EventId event_id, Object* data, bool sync_op /*= false */ ):
    m_handle( handle ),
    m_event_id( event_id ),
    m_sync( sync_op ),
    m_result( NULL )
{
    m_data.m_object = data;
    m_data_is_object = true;

    if ( data )
    {
        data->get_ref( );
    }

    ASSERT_DEBUG( handle );
    if ( m_sync )
    {
        m_signal << NEW OsSemaphore;
    }

    reset_stamp( );
}

// -------------------------------------------------------------------------------------------------
// Set stamp for Ev_Sys_OnEntry, Ev_Sys_Heartbeat and Ev_Sys_LastHeartbeat only
// -------------------------------------------------------------------------------------------------
inline
void Event::reset_stamp( ) 
{
    if ( ( m_event_id & StateMachine::sysEventMask ) &&
         ( m_event_id == ( int )0x80000002 ||
           m_event_id == ( int )0x80000003 ||
           m_event_id == ( int )0x80000004 ) )
    {
        m_stamp = m_handle->get_state_stamp( );
    }
    else
    {
        m_stamp = -1;
    }
}

// -------------------------------------------------------------------------------------------------
